package com.ling.pl.autoproject.definition.service;

import com.ling.pl.autoproject.definition.dao.ProgrammeDao;
import com.ling.pl.autoproject.definition.model.ProgrammeEntity;
import com.ling.pl.autoproject.definition.model.ProgrammeVO;
import com.ling.pl.autoproject.definition.model.ProjectEntity;
import com.ling.pl.autoproject.definition.model.ProjectVO;
import com.ling.pl.core.commons.support.DaoPage;
import com.ling.pl.core.commons.utils.BatchUtils;
import com.ling.pl.core.dao.IDao;
import com.ling.pl.core.service.impl.BaseService;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bo.wang
 */
public abstract class BaseProgrammeService extends BaseService {

    public static final String BEAN_ID = "programmeService";

    @Resource
    ProgrammeDao programmeDao;


    public IDao getDao() {
        return programmeDao;
    }


    public DaoPage findProgrammeVOByParams(Map params, Integer pageIndex,
                                           Integer pageSize) {
        if (params == null) {
            params = new HashMap();
        }
        DaoPage daoPage = programmeDao.findProgrammeByParams(params, pageIndex, pageSize);
        daoPage.setResult(convertProgrammeToVO((List<ProgrammeEntity>) daoPage.getResult()));
        return daoPage;
    }

    public List<ProgrammeVO> findProgrammeVOByParams(Map params) {
        if (params == null) {
            params = new HashMap();
        }
        List<ProgrammeEntity> temp = programmeDao.findProgrammeByParams(params);
        return convertProgrammeToVO(temp);
    }

    public DaoPage findProgrammeByParams(Map params, Integer pageIndex,
                                         Integer pageSize) {
        if (params == null) {
            params = new HashMap();
        }
        DaoPage daoPage = programmeDao.findProgrammeByParams(params, pageIndex, pageSize);
        return daoPage;
    }

    public List<ProgrammeEntity> findProgrammeByParams(Map params) {
        if (params == null) {
            params = new HashMap();
        }
        List<ProgrammeEntity> temp = programmeDao.findProgrammeByParams(params);
        return temp;
    }


    public void saveProgrammeDataListsMap(Map dataListsMap) {
        Collection<ProgrammeVO> deleteList = BatchUtils.to(ProgrammeVO.class).getDeleteEntities(dataListsMap);
        Collection<ProgrammeVO> insertList = BatchUtils.to(ProgrammeVO.class).getInsertEntities(dataListsMap);
        Collection<ProgrammeVO> updateList = BatchUtils.to(ProgrammeVO.class).getModifiedEntities(dataListsMap);
        if (updateList != null && updateList.size() > 0) {
            for (ProgrammeVO vo : updateList) {
                ProgrammeEntity entity = convertProgrammeVOToEntity(vo);
                programmeDao.update(entity);
            }
//			programmeDao.updateAll(updateList);
        }
        if (insertList != null && insertList.size() > 0) {
            for (ProgrammeVO vo : insertList) {
                ProgrammeEntity entity = convertProgrammeVOToEntity(vo);
                programmeDao.save(entity);
                vo.setId(entity.getId());
            }
//			programmeDao.saveAll(insertList);
        }
        if (deleteList != null && deleteList.size() > 0) {
            for (ProgrammeVO vo : deleteList) {
                ProgrammeEntity entity = new ProgrammeEntity();
                entity.setId(vo.getId());
                programmeDao.remove(entity);
            }
//			programmeDao.removeAll(deleteList);
        }

    }

    protected abstract List<ProgrammeVO> convertProgrammeToVO(List<ProgrammeEntity> models);

    public abstract ProgrammeVO convertProgrammeToVO(ProgrammeEntity model);

    public abstract ProgrammeEntity convertProgrammeVOToEntity(ProgrammeVO vo);

    public DaoPage findProjectVOByParams(Map params, Integer pageIndex,
                                         Integer pageSize) {
        if (params == null) {
            params = new HashMap();
        }
        DaoPage daoPage = programmeDao.findProjectByParams(params, pageIndex, pageSize);
        daoPage.setResult(convertProjectToVO((List<ProjectEntity>) daoPage.getResult()));
        return daoPage;
    }

    public List<ProjectVO> findProjectVOByParams(Map params) {
        if (params == null) {
            params = new HashMap();
        }
        List<ProjectEntity> temp = programmeDao.findProjectByParams(params);
        return convertProjectToVO(temp);
    }

    public DaoPage findProjectByParams(Map params, Integer pageIndex,
                                       Integer pageSize) {
        if (params == null) {
            params = new HashMap();
        }
        DaoPage daoPage = programmeDao.findProjectByParams(params, pageIndex, pageSize);
        return daoPage;
    }

    public List<ProjectEntity> findProjectByParams(Map params) {
        if (params == null) {
            params = new HashMap();
        }
        return programmeDao.findProjectByParams(params);
    }

    public void saveProjectDataListsMap(Map dataListsMap) {
        Collection<ProjectVO> deleteList = BatchUtils.to(ProjectVO.class).getDeleteEntities(dataListsMap);
        Collection<ProjectVO> insertList = BatchUtils.to(ProjectVO.class).getInsertEntities(dataListsMap);
        Collection<ProjectVO> updateList = BatchUtils.to(ProjectVO.class).getModifiedEntities(dataListsMap);
        if (updateList != null && updateList.size() > 0) {
            for (ProjectVO vo : updateList) {
                ProjectEntity entity = convertProjectVOToEntity(vo);
                programmeDao.update(entity);
            }
//			programmeDao.updateAll(updateList);
        }
        if (insertList != null && insertList.size() > 0) {
            for (ProjectVO vo : insertList) {
                ProjectEntity entity = convertProjectVOToEntity(vo);
                programmeDao.save(entity);
                vo.setId(entity.getId());
            }
//			programmeDao.saveAll(insertList);
        }
        if (deleteList != null && deleteList.size() > 0) {
            for (ProjectVO vo : deleteList) {
                ProjectEntity entity = new ProjectEntity();
                entity.setId(vo.getId());
                programmeDao.remove(entity);
            }
//			programmeDao.removeAll(deleteList);
        }
    }

    protected abstract List<ProjectVO> convertProjectToVO(List<ProjectEntity> models);

    public abstract ProjectVO convertProjectToVO(ProjectEntity model);

    public abstract ProjectEntity convertProjectVOToEntity(ProjectVO vo);
}

